package com.bfly.mall.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.bfly.common.ResponseUtil;
import com.bfly.common.util.DateUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.report.dto.DataValueDTO;
import com.bfly.mall.report.service.IReportService;
import com.bfly.mall.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员统计
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/10/16 20:46
 */
@RestController
@RequestMapping(value = "/manage/report/user")
public class UserReportController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IReportService reportService;

    /**
     * 会员综合统计
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/6/4 11:35
     */
    @GetMapping(value = "/statistics")
    @PermissionCheck(required = false)
    public void statisticsUser(HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();

        // 今日会员注册数
        Date beginDate = DateUtil.getCurrentDayMorning();
        Date endDate = DateUtil.getCurrentDayNight();
        param.put("create_date_beginDate", beginDate);
        param.put("create_date_endDate", endDate);
        long today = userService.getCount(param);

        JSONObject json = new JSONObject();
        json.put("today", today);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(json));
    }

    /**
     * 统计用户增长趋势
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/9 12:25
     */
    @GetMapping(value = "/increase")
    public void statisticsUserIncrease(@RequestParam("start") Date start, @RequestParam("end") Date end, HttpServletResponse response) {
        List<DataValueDTO> list = reportService.statisticsUserIncrease(start, end);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }
}
